<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

/**
 * permission 权限操作
 */
class Permission
{

    /**
     * 缓存文件存放位置
     * @var string
     */
    private $_cachePath;

    /**
     * 缓存文件命名规则
     * @var string
     */
    private $_cacheFilePerfix = 'group_';

    /**
     * 权限前缀
     * @var string 
     */
    private $_permissionPerfix = 'manage';
    
    /**
     * 权限数据
     */
    private $_permissionData;

    /**
     * 缓存过期时间(秒) 默认5分钟
     */
    private $_expireTime = 300;

    /**
     * CI实体
     * @var object 
     */
    private $_CI;

    function __construct( $cachePath = '' )
    {
        $this->_CI = & get_instance();
        $this->_cachePath = is_dir( $cachePath ) ? $cachePath : APPPATH . 'cache/';
        $this->_init();
    }

    /**
     * 初始化
     */
    private function _init()
    {
        $groupid = intval( $this->_CI->session->getUserSession( 'groupid' ) );
        if ( empty( $groupid ) ) return false;

        $cacheFileName = $this->getCachePath() . $this->_cacheFilePerfix . $groupid . '.php';
        if ( $this->_cacheValid( $cacheFileName ) )
        {
            include( $cacheFileName );
            $this->_permissionData = isset( $group ) ? $group : array( );
        }
        else
        {
            //更新缓存
            $this->_permissionData = $this->updateCache( $cacheFileName, $groupid );
        }
    }

    /**
     * 更新权限缓存数据
     * @param string $cacheFileName 权限文件名
     * @param int $groupid 组ID
     * @return array 组权限数据
     */
    public function updateCache( $cacheFileName, $groupid )
    {
        $this->_CI->load->model( 'group_model' );
        $groupData = $this->_CI->group_model->getByGroupId( $groupid );
        $handle = @fopen( $cacheFileName, 'wb+' );
        if ( empty( $handle ) ) return $groupData;
        $string = "<?php\n\$group=" . var_export( $groupData, TRUE ) . "\n?>";
        fwrite( $handle, $string );
        fclose( $handle );
        return $groupData;
    }

    /**
     * 缓存是否有效
     * @param string $cacheFileName 缓存文件
     * @return bool
     */
    private function _cacheValid( $cacheFileName )
    {
        return file_exists( $cacheFileName ) && ( time() - filemtime( $cacheFileName ) < $this->_expireTime );
    }

    /**
     * 获取缓存放置位置
     * @return string
     */
    public function getCachePath()
    {
        return $this->_cachePath;
    }

    /**
     * 获取权限数据
     * @return array
     */
    public function getPermissionData()
    {
        return $this->_permissionData;
    }
    
    /**
     * 判断是否具有某个权限
     * @param string $permission
     * @return bool
     */
    public function chkPermission( $permission )
    {
        $permissionData = $this->getPermissionData();
        return isset( $permissionData[$this->_permissionPerfix . $permission] ) ? $permissionData[$this->_permissionPerfix . $permission] : 0;
    }
}